package com.gin.no_gay_alliance.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.adpter.topic.TopicItemAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.database.dao.TopicHistoryDao;
import com.gin.no_gay_alliance.database.entity.TopicHistory;
import com.gin.no_gay_alliance.databinding.ActivityTopicHistoryBinding;
import com.gin.no_gay_alliance.listener.MyScrollListener;
import com.gin.no_gay_alliance.viewmodel.TopicItem;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 主题浏览历史
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/15 16:33
 **/
public class TopicHistoryActivity extends BaseBindingActivity<ActivityTopicHistoryBinding> implements TextView.OnEditorActionListener, MyScrollListener.OnScrollToBottomListener {
    int page = 1;
    int size = 20;
    private TopicItemAdapter adapter;

    /**
     * 解析意图
     *
     * @param intent 意图
     */
    @Override
    protected void handleIntent(Intent intent) {

    }

    /**
     * onCreate中的后续操作
     */
    @Override
    protected void onCreate() {
        // 绑定软键盘搜索键
        b.et.setOnEditorActionListener(this);

        // 适配器
        adapter = new TopicItemAdapter(this);
        b.recyclerView.setAdapter(adapter);
        MyScrollListener listener = new MyScrollListener();
        listener.setOnScrollToBottomListener(this);
        b.recyclerView.addOnScrollListener(listener);

        load(true);
    }

    /**
     * 装载右上角菜单
     *
     * @param menu         菜单
     * @param menuInflater 菜单渲染器
     */
    @Override
    protected void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
        menuInflater.inflate(R.menu.search_menu, menu);
    }

    @Override
    protected Toolbar initToolbar() {
        return b.toolbar;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.menu_item_search) {
            load(false);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        // 当按下软键盘搜索键时，执行搜索
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            // 执行搜索
            load(false);
            return true;
        }
        return false;
    }

    /**
     * 已滚动到底部
     */
    @Override
    public void onScrollToBottom() {
        load(true);
    }

    /**
     * 是否为新搜索
     *
     * @param more 是否为加载下一页，如果不是，清空原数据
     */
    @SuppressLint("NotifyDataSetChanged")
    private void load(boolean more) {
        if (!more) {
            page = 1;
            ui(() -> {
                adapter.clear();
                adapter.notifyDataSetChanged();
            });
        }
        TopicHistoryDao dao = MyApplication.app.ngaDatabase.topicHistoryDao();
        String text = String.valueOf(b.et.getText());
        String keyword = ObjectUtils.isEmpty(text) ? null : text;
        // 查询
        List<TopicHistory> list = dao.list(keyword, page, size);
        if (list.size() > 0) {
            page++;
            //添加数据
            List<TopicItem> data = list.stream().map(TopicHistory::toTopicItem).collect(Collectors.toList());
            ui(() -> adapter.pushData(data));
        } else {
            toast("没有更多数据了");
        }
    }

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, TopicHistoryActivity.class));
    }

}
